﻿namespace SugarTank.Mobile.Managers
{
    using System;
    using System.Linq;
    using CommonTypes.Phone;
    using Contracts;
    using Microsoft.Phone.Tasks;

    public class LauncherManager : ILauncherManager
    {
        #region ILauncherManager Members

        /// <summary>
        /// Composes the email.
        /// </summary>
        /// <param name="email">The email.</param>
        public void ComposeEmail(Email email)
        {
            var emailComposeTask = new EmailComposeTask();
            emailComposeTask.To = string.Join(";", email.To.ToArray());
            emailComposeTask.Cc = string.Join(";", email.Cc.ToArray());
            emailComposeTask.Body = email.Body;
            emailComposeTask.Subject = email.Subject;
            emailComposeTask.Show();
        }

        /// <summary>
        /// Composes the SMS.
        /// </summary>
        /// <param name="sms">The SMS.</param>
        public void ComposeSms(Sms sms)
        {
            var smsComposeTask = new SmsComposeTask();
            smsComposeTask.To = sms.To;
            smsComposeTask.Body = sms.Body;
        }

        /// <summary>
        /// Makes the call.
        /// </summary>
        /// <param name="phoneNumber">The phone call.</param>
        public void MakeCall(PhoneNumber phoneNumber)
        {
            var phoneCallTask = new PhoneCallTask();
            phoneCallTask.PhoneNumber = phoneNumber.Number;
            phoneCallTask.DisplayName = phoneNumber.DisplayName;
            phoneCallTask.Show();
        }

        /// <summary>
        /// Launches the web browser.
        /// </summary>
        /// <param name="uri">The URI.</param>
        public void LaunchWebBrowser(Uri uri)
        {
            var webBrowserTask = new WebBrowserTask();
            webBrowserTask.URL = uri.ToString();
            webBrowserTask.Show();
        }

        /// <summary>
        /// Launches the search application.
        /// </summary>
        /// <param name="query">The query.</param>
        public void LaunchSearch(string query)
        {
            var searchTask = new SearchTask();
            searchTask.SearchQuery = query;
            searchTask.Show();
        }

        /// <summary>
        /// Shows the market place details.
        /// </summary>
        /// <param name="contentId">The content id.</param>
        public void LaunchMarketplaceDetails(string contentId)
        {
            var marketplaceDetailTask = new MarketplaceDetailTask();
            marketplaceDetailTask.ContentIdentifier = contentId;
            marketplaceDetailTask.ContentType = MarketplaceContentType.Applications;
            marketplaceDetailTask.Show();
        }

        /// <summary>
        /// Shows the market place hub.
        /// </summary>
        /// <param name="contentType">Type of the content.</param>
        public void LaunchMarketplaceHub(MarketplaceContentType contentType)
        {
            var marketplaceHubTask = new MarketplaceHubTask();
            marketplaceHubTask.ContentType = contentType;
            marketplaceHubTask.Show();
        }

        /// <summary>
        /// Shows the marketplace review.
        /// </summary>
        public void LaunchMarketplaceReview()
        {
            var marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
        }

        /// <summary>
        /// Searches the marketplace.
        /// </summary>
        /// <param name="search"></param>
        public void SearchMarketplace(MarketplaceSearch search)
        {
            var marketplaceSearchTask = new MarketplaceSearchTask();
            marketplaceSearchTask.ContentType = search.ContentType;
            marketplaceSearchTask.SearchTerms = search.Query;
            marketplaceSearchTask.Show();
        }

        #endregion
    }
}